package cn.cttic.base.service.impl;

import cn.cttic.base.api.domain.BaseCompanyProduct;
import cn.cttic.common.core.utils.MapstructUtils;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import cn.cttic.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.base.api.domain.bo.BaseDeviceInfoBo;
import cn.cttic.base.api.domain.vo.BaseDeviceInfoVo;
import cn.cttic.base.api.domain.BaseDeviceInfo;
import cn.cttic.base.mapper.BaseDeviceInfoMapper;
import cn.cttic.base.service.IBaseDeviceInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备信息Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-10-24
 */
@RequiredArgsConstructor
@Service
public class BaseDeviceInfoServiceImpl implements IBaseDeviceInfoService {

    private final BaseDeviceInfoMapper baseMapper;

    /**
     * 查询设备信息
     *
     * @param id 主键
     * @return 设备信息
     */
    @Override
    public BaseDeviceInfoVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询设备信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 设备信息分页列表
     */
    @Override
    public TableDataInfo<BaseDeviceInfoVo> queryPageList(BaseDeviceInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseDeviceInfo> lqw = buildQueryWrapper(bo);
        Page<BaseDeviceInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的设备信息列表
     *
     * @param bo 查询条件
     * @return 设备信息列表
     */
    @Override
    public List<BaseDeviceInfoVo> queryList(BaseDeviceInfoBo bo) {
        LambdaQueryWrapper<BaseDeviceInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseDeviceInfo> buildQueryWrapper(BaseDeviceInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseDeviceInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(BaseDeviceInfo::getCreateTime);
        lqw.eq(bo.getThirdId() != null, BaseDeviceInfo::getThirdId, bo.getThirdId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNum()), BaseDeviceInfo::getBatchNum, bo.getBatchNum());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageId()), BaseDeviceInfo::getMessageId, bo.getMessageId());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalImei()), BaseDeviceInfo::getTerminalImei, bo.getTerminalImei());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalIccid()), BaseDeviceInfo::getTerminalIccid, bo.getTerminalIccid());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalProtocol()), BaseDeviceInfo::getTerminalProtocol, bo.getTerminalProtocol());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalType()), BaseDeviceInfo::getTerminalType, bo.getTerminalType());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalModel()), BaseDeviceInfo::getTerminalModel, bo.getTerminalModel());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalMfrs()), BaseDeviceInfo::getTerminalMfrs, bo.getTerminalMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalMfrsAbbreviation()), BaseDeviceInfo::getTerminalMfrsAbbreviation, bo.getTerminalMfrsAbbreviation());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalMfrsProvinceNum()), BaseDeviceInfo::getTerminalMfrsProvinceNum, bo.getTerminalMfrsProvinceNum());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalMfrsNum()), BaseDeviceInfo::getTerminalMfrsNum, bo.getTerminalMfrsNum());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalHardwareVersion()), BaseDeviceInfo::getTerminalHardwareVersion, bo.getTerminalHardwareVersion());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminalSoftwareVersion()), BaseDeviceInfo::getTerminalSoftwareVersion, bo.getTerminalSoftwareVersion());
        lqw.eq(StringUtils.isNotBlank(bo.getProductionBatch()), BaseDeviceInfo::getProductionBatch, bo.getProductionBatch());
        lqw.eq(StringUtils.isNotBlank(bo.getProductionDate()), BaseDeviceInfo::getProductionDate, bo.getProductionDate());
        lqw.eq(bo.getCompanyId() != null, BaseDeviceInfo::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), BaseDeviceInfo::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getSeMfrs()), BaseDeviceInfo::getSeMfrs, bo.getSeMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getSeMfrsId()), BaseDeviceInfo::getSeMfrsId, bo.getSeMfrsId());
        lqw.eq(StringUtils.isNotBlank(bo.getSeModel()), BaseDeviceInfo::getSeModel, bo.getSeModel());
        lqw.eq(StringUtils.isNotBlank(bo.getSeId()), BaseDeviceInfo::getSeId, bo.getSeId());
        lqw.eq(StringUtils.isNotBlank(bo.getGnssChipMfrs()), BaseDeviceInfo::getGnssChipMfrs, bo.getGnssChipMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getGnssChipModel()), BaseDeviceInfo::getGnssChipModel, bo.getGnssChipModel());
        lqw.eq(StringUtils.isNotBlank(bo.getGnssAntennaMfrs()), BaseDeviceInfo::getGnssAntennaMfrs, bo.getGnssAntennaMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getGnssAntennaModel()), BaseDeviceInfo::getGnssAntennaModel, bo.getGnssAntennaModel());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunicationModuleMfrs()), BaseDeviceInfo::getCommunicationModuleMfrs, bo.getCommunicationModuleMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunicationModuleModel()), BaseDeviceInfo::getCommunicationModuleModel, bo.getCommunicationModuleModel());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunicationAntennaMfrs()), BaseDeviceInfo::getCommunicationAntennaMfrs, bo.getCommunicationAntennaMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunicationAntennaModel()), BaseDeviceInfo::getCommunicationAntennaModel, bo.getCommunicationAntennaModel());
        lqw.eq(StringUtils.isNotBlank(bo.getInertialSensorMfrs()), BaseDeviceInfo::getInertialSensorMfrs, bo.getInertialSensorMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getInertialSensorModel()), BaseDeviceInfo::getInertialSensorModel, bo.getInertialSensorModel());
        lqw.eq(StringUtils.isNotBlank(bo.getBluetoothMfrs()), BaseDeviceInfo::getBluetoothMfrs, bo.getBluetoothMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getBluetoothModel()), BaseDeviceInfo::getBluetoothModel, bo.getBluetoothModel());
        lqw.eq(StringUtils.isNotBlank(bo.getBluetoothVersion()), BaseDeviceInfo::getBluetoothVersion, bo.getBluetoothVersion());
        lqw.eq(StringUtils.isNotBlank(bo.getBluetoothMac()), BaseDeviceInfo::getBluetoothMac, bo.getBluetoothMac());
        lqw.eq(StringUtils.isNotBlank(bo.getSpareBatteryType()), BaseDeviceInfo::getSpareBatteryType, bo.getSpareBatteryType());
        lqw.eq(StringUtils.isNotBlank(bo.getSpareBatteryMfrs()), BaseDeviceInfo::getSpareBatteryMfrs, bo.getSpareBatteryMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getSpareBatteryVersion()), BaseDeviceInfo::getSpareBatteryVersion, bo.getSpareBatteryVersion());
        lqw.eq(StringUtils.isNotBlank(bo.getEtcObuMfrsId()), BaseDeviceInfo::getEtcObuMfrsId, bo.getEtcObuMfrsId());
        lqw.eq(StringUtils.isNotBlank(bo.getEtcObuMfrs()), BaseDeviceInfo::getEtcObuMfrs, bo.getEtcObuMfrs());
        lqw.eq(StringUtils.isNotBlank(bo.getEtcObuModel()), BaseDeviceInfo::getEtcObuModel, bo.getEtcObuModel());
        lqw.eq(StringUtils.isNotBlank(bo.getEtcObuId()), BaseDeviceInfo::getEtcObuId, bo.getEtcObuId());
        lqw.eq(StringUtils.isNotBlank(bo.getEtcObuCardNum()), BaseDeviceInfo::getEtcObuCardNum, bo.getEtcObuCardNum());
        lqw.eq(StringUtils.isNotBlank(bo.getUseruuid()), BaseDeviceInfo::getUseruuid, bo.getUseruuid());
        lqw.eq(StringUtils.isNotBlank(bo.getLastUpdateTime()), BaseDeviceInfo::getLastUpdateTime, bo.getLastUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLastUpdateUseruuid()), BaseDeviceInfo::getLastUpdateUseruuid, bo.getLastUpdateUseruuid());
        lqw.eq(bo.getState() != null, BaseDeviceInfo::getState, bo.getState());
        lqw.eq(bo.getTerminalStockLogId() != null, BaseDeviceInfo::getTerminalStockLogId, bo.getTerminalStockLogId());
        lqw.eq(bo.getTerminalStockLogId1() != null, BaseDeviceInfo::getTerminalStockLogId1, bo.getTerminalStockLogId1());
        lqw.eq(bo.getStatus() != null, BaseDeviceInfo::getStatus, bo.getStatus());
        lqw.eq(bo.getIsUse() != null, BaseDeviceInfo::getIsUse, bo.getIsUse());
        lqw.eq(bo.getIsActivated() != null, BaseDeviceInfo::getIsActivated, bo.getIsActivated());
        lqw.eq(bo.getIsConfirmOut() != null, BaseDeviceInfo::getIsConfirmOut, bo.getIsConfirmOut());
        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsOrderNumber()), BaseDeviceInfo::getLogisticsOrderNumber, bo.getLogisticsOrderNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEsimModel()), BaseDeviceInfo::getEsimModel, bo.getEsimModel());
        lqw.eq(bo.getSupportEtc() != null, BaseDeviceInfo::getSupportEtc, bo.getSupportEtc());
        lqw.eq(bo.getOutboundid() != null, BaseDeviceInfo::getOutboundid, bo.getOutboundid());
        lqw.eq(StringUtils.isNotBlank(bo.getModified()), BaseDeviceInfo::getModified, bo.getModified());
        lqw.eq(bo.getIsCamera() != null, BaseDeviceInfo::getIsCamera, bo.getIsCamera());
        lqw.eq(bo.getCameraCount() != null, BaseDeviceInfo::getCameraCount, bo.getCameraCount());
        lqw.eq(bo.getIsWifi() != null, BaseDeviceInfo::getIsWifi, bo.getIsWifi());
        lqw.eq(StringUtils.isNotBlank(bo.getStockStatus()), BaseDeviceInfo::getStockStatus, bo.getStockStatus());
        lqw.eq(bo.getCreatedAt() != null, BaseDeviceInfo::getCreatedAt, bo.getCreatedAt());
        lqw.eq(bo.getUpdatedAt() != null, BaseDeviceInfo::getUpdatedAt, bo.getUpdatedAt());
        return lqw;
    }

    /**
     * 新增设备信息
     *
     * @param bo 设备信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseDeviceInfoBo bo) {
        BaseDeviceInfo add = MapstructUtils.convert(bo, BaseDeviceInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备信息
     *
     * @param bo 设备信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseDeviceInfoBo bo) {
        BaseDeviceInfo update = MapstructUtils.convert(bo, BaseDeviceInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseDeviceInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除设备信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
